<template>
  <el-menu
    class="sidebar-menu"
    :collapse="false"
    router
    background-color="#304156"
    text-color="#bfcbd9"
    active-text-color="#409EFF"
  >
    <el-menu-item index="/" class="menu-item">
      <el-icon><ChatLineRound /></el-icon>
    </el-menu-item>
    <el-menu-item index="/app-center" class="menu-item">
        <el-icon><Menu /></el-icon>
    </el-menu-item>
    <el-menu-item index="/about" class="menu-item">
      <el-icon><InfoFilled /></el-icon>
    </el-menu-item>

    <!-- 新增登录按钮 -->
    <el-menu-item class="menu-item login-item" @click="showLoginDialog">
      <el-icon><User /></el-icon>
    </el-menu-item>
  </el-menu>

  <!-- 登录弹窗 -->
  <el-dialog v-model="loginVisible" title="用户登录" width="30%">
    <el-form :model="loginForm">
      <el-form-item label="用户名">
        <el-input v-model="loginForm.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="loginForm.password" type="password" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="loginVisible = false">取消</el-button>
      <el-button type="primary" @click="handleLogin">登录</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ChatLineRound, InfoFilled, Menu, User } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useStore } from 'vuex'
import { post } from '@/utils/request'

const store = useStore()
const loginVisible = ref(false)
const loginForm = ref({
  username: '',
  password: ''
})

const showLoginDialog = () => {
  loginVisible.value = true
}

const handleLogin = async () => {
  try {
    const res = await post('/login', loginForm.value)
    if (res.code === 200) {
      store.commit('setSmartNeroUser', {
        token: res.data.token,
        isGuest: false,
        userInfo: res.data.user
      })
      loginVisible.value = false
    }
  } catch (error) {
    console.error('登录失败:', error)
  }
}
</script>

<style scoped>
.sidebar-menu {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 68px;
  z-index: 1000;
}

.menu-item {
  height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.menu-item .el-icon {
  font-size: 28px;
  margin-bottom: 8px;
}

.menu-item span {
  font-size: 14px;
}

.login-item {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>